<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: product_attributes.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require(DIR_FS_INC.'inc.get_hex_image.php');

$module_smarty = new Smarty();
$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');

if($product->getAttributesCount() > 0) {
	$options = $db->db_query("SELECT
									popt.products_options_id,
									popt.products_options_name,
									popt.products_options_desc,
									popt.products_options_image,
									patrib.options_view_type
								FROM
									".TABLE_PRODUCTS_OPTIONS." popt,
									".TABLE_PRODUCTS_ATTRIBUTES." patrib
								WHERE
									patrib.products_id = '".(int)$product->data['products_id']."'
								AND
									patrib.options_id = popt.products_options_id
								AND
									popt.language_id = '".(int)$_SESSION['languages_id']."'
								GROUP BY
									popt.products_options_id
								ORDER BY
									patrib.sortorder", true);

	$row = 0;
	$col = 0;
	$products_options_data = array ();
	while(!$options->EOF) {
		$selected = 0;
		$products_options_array = array ();

		$products_options_data[$row] = array('ID' => $options->fields['products_options_id'],
											'NAME' => $options->fields['products_options_name'],
											'DESC' => nl2br($options->fields['products_options_desc']),
											'IMAGE' => ($options->fields['products_options_image'] !='' ? DIR_WS_IMAGES.'product_options/'.$options->fields['products_options_image'] : ''),
											'IMAGE_DIMENSION' => ($options->fields['products_options_image'] !='' ? get_img_size(DIR_WS_IMAGES.'product_options/'.$options->fields['products_options_image']) : ''),
											'VIEW' => CURRENT_TEMPLATE.'/html/options_'.($options->fields['options_view_type']!='-'?$options->fields['options_view_type']:'d').'.html',
											'VIEW_RAW' => ($options->fields['options_view_type']!='-'?$options->fields['options_view_type']:'d'),
											'DATA' => '');

		$products_options = $db->db_query("SELECT
												pov.products_options_values_id,
												pov.products_options_values_name,
												pov.products_options_values_desc,
												pov.products_options_values_image,
												pa.attributes_model,
												pa.options_values_price,
												pa.options_values_weight,
												pa.options_values_weight_status,
												pa.price_prefix,
												pa.attributes_stock,
												pa.attributes_stock_status,
												pa.attributes_model
                                            FROM
                                            	".TABLE_PRODUCTS_ATTRIBUTES." pa,
                                                ".TABLE_PRODUCTS_OPTIONS_VALUES." pov
                                            WHERE
                                            	pa.products_id = '".(int)$product->data['products_id']."'
                                            AND
                                            	pa.options_id = '".(int)$options->fields['products_options_id']."'
                                            AND
                                            	pa.options_values_id = pov.products_options_values_id
                                            AND
                                            	pov.language_id = '".(int)$_SESSION['languages_id']."'
                                            ORDER BY
                                            	pa.sortorder", true);
		$col = 0;
		while(!$products_options->EOF) {
			$attr_price = '';
			$weight = '';
			
			if($products_options->fields['options_values_weight_status'] == 1)
				$weight = ' <img class="product_weight_img" '.get_img_size('images/icons/weight_icon.png').' src="images/icons/weight_icon.png" alt="" title="'.round($products_options->fields['options_values_weight'], 2).'kg" />';
			
			if ($_SESSION['customers_status']['customers_status_show_price'] == '0') {
				$products_options_data[$row]['DATA'][$col] = array ('ID' => $products_options->fields['products_options_values_id'],
																	'TEXT' => nl2br($products_options->fields['products_options_values_name']),
																	'MODEL' => $products_options->fields['attributes_model'],
																	'IMAGE' => ($products_options->fields['products_options_values_image'] !='' ? DIR_WS_IMAGES.'product_options/'.$products_options->fields['products_options_values_image'] : ''),
																	'IMAGE_DIMENSION' => ($products_options->fields['products_options_values_image'] !='' ? get_img_size(DIR_WS_IMAGES.'product_options/'.$products_options->fields['products_options_values_image']) : ''),
																	'PRICE' => '',
																	'FULL_PRICE' => '',
																	'HEX_IMAGE' => get_hex_image($products_options->fields['products_options_values_id']),
																	'STOCK_IMG' => ($products_options->fields['attributes_stock_status'] == 1 ? $product->getStockImg($products_options->fields['attributes_stock']) : ''),
																	'WEIGHT' => $weight,
																	'PREFIX' => $products_options->fields['price_prefix']);

			} else {
				if ($products_options->fields['options_values_price'] != '0.00')
					$attr_price = $price->format($products_options->fields['options_values_price'], false, $product->data['products_tax_class_id']);

				$products_price = $price->GetPrice($product->data['products_id'], false, 1, $product->data['products_tax_class_id'], $product->data['products_price']);

				if ($_SESSION['customers_status']['customers_status_discount_attributes'] == 1 && $products_options->fields['price_prefix'] == '+')
					$attr_price -= $price / 100 * $discount;

				if ($products_options->fields['price_prefix']=="-")
					$attr_price_full = $attr_price*(-1);
				$full = $products_price + $attr_price_full;
					$products_options_data[$row]['DATA'][$col] = array ('ID' => $products_options->fields['products_options_values_id'],
																		'TEXT' => $products_options->fields['products_options_values_name'],
																		'DESC' => $products_options->fields['products_options_values_desc'],
																		'IMAGE' => ($products_options->fields['products_options_values_image'] !='' ? DIR_WS_IMAGES.'product_options/'.$products_options->fields['products_options_values_image'] : ''),
																		'MODEL' => $products_options->fields['attributes_model'],
																		'STOCK_IMG' => ($products_options->fields['attributes_stock_status'] == 1 ? $product->getStockImg($products_options->fields['attributes_stock']) : ''),
																		'WEIGHT' => $weight,
																		'HEX_IMAGE' => get_hex_image($products_options->fields['products_options_values_id']),
																		'PRICE' => $price->format($attr_price, true),
																		'FULL_PRICE' => $price->format($full, true),
																		'PREFIX' => $products_options->fields['price_prefix']);

				if ($attr_price == 0) {
					unset($products_options_data[$row]['DATA'][$col]['PRICE']);
					unset($products_options_data[$row]['DATA'][$col]['PREFIX']);
				}
			}
			$col ++;
			$products_options->MoveNext();
		}
		$row ++;
		$options->MoveNext();
	}
}

if($product->data['options_template'] == '' || $product->data['options_template'] == 'default') {
	$files = array();
	if ($dir = opendir(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/product_options/')) {
		while (($file = readdir($dir)) !== false) {
			if (is_file(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/product_options/'.$file) && ($file != "index.html") and (substr($file, 0, 1) !="."))
				$files[] = array('id' => $file, 'text' => $file);
		}
		closedir($dir);
		asort($files);
		reset($files);
	}
	$product->data['options_template'] = $files[0]['id'];
}

$module_smarty->assign('language', $_SESSION['language']);
$module_smarty->assign('options', $products_options_data);

$module_smarty->caching = false;
$module = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/product_options/'.$product->data['options_template']);

$info_smarty->assign('MODULE_product_options', $module);